﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XiaoYuStock.Core
{
    /// <summary>
    ///  autor：tiny
    ///  time：2018-06-21
    ///  description：api请求结果对象，返回一个list+分页参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ApiResultList<T> : BaseApiResult
    {
        /// <summary>
        ///  autor：tiny
        ///  time：2018-06-21
        ///  description：构造函数，初始化默认分页参数
        /// </summary>
        public ApiResultList() : base()
        {
            this.PageSize = 20;
            this.PageIndex = 0;
            this.TotalCout = 0;
        }


        /// <summary>
        /// 结果数据
        /// </summary>
        public List<T> Data { get; set; }

        /// <summary>
        ///  autor：tiny
        ///  time：2018-06-20
        ///  description：页码大小
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 页码
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 总条数
        /// </summary>
        public int TotalCout { get; set; }


        #region 请求成功

        /// <summary>
        ///  autor：tiny
        ///  time：2018-06-20
        ///  description：请求成功
        /// </summary>
        /// <param name="data">结果数据</param>
        /// <param name="pageSize">页码大小</param>
        /// <param name="pageIndex">分页参数</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public ApiResultList<T> Success(List<T> data, int pageSize = 0, int pageIndex = 0, int totalCount = 0)
        {
            base.Success();

            this.Data = data ?? new List<T>();

            if (pageSize > 0)
            {
                this.PageSize = pageSize;
            }

            this.PageIndex = pageIndex;
            if (totalCount > 0)
            {
                this.TotalCout = totalCount;
            }
            else
            {
                this.TotalCout = this.Data.Count;
            }
            return this;
        }

        /// <summary>
        ///  autor：tiny
        ///  time：2018-06-20
        ///  description：请求成功
        /// </summary>
        /// <param name="data">结果数据</param>
        /// <param name="subCode">子状态码</param>
        /// <param name="pageSize">页码大小</param>
        /// <param name="pageIndex">分页参数</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public ApiResultList<T> Success(List<T> data, ApiResultSubCode subCode, int pageSize = 0, int pageIndex = 0, int totalCount = 0)
        {
            base.Success(subCode);

            this.Data = data ?? new List<T>();

            if (pageSize > 0)
            {
                this.PageSize = pageSize;
            }

            this.PageIndex = pageIndex;
            if (totalCount > 0)
            {
                this.TotalCout = totalCount;
            }
            else
            {
                this.TotalCout = this.Data.Count;
            }
            return this;
        }


        #endregion

        #region 请求失败

        /// <summary>
        ///  autor：tiny
        ///  time：2018-06-20
        ///  description：请求失败
        /// </summary>
        /// <returns></returns>
        public new ApiResultList<T> Error()
        {
            base.Error();
            return this;
        }


        /// <summary>
        ///  autor：tiny
        ///  time：2018-06-20
        ///  description：请求失败
        /// </summary>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public new ApiResultList<T> Error(string msg)
        {
            base.Error(msg);
            return this;
        }


        /// <summary>
        ///  autor：tiny
        ///  time：2018-06-20
        ///  description：请求失败
        /// </summary>
        /// <param name="subCode">子状态码</param>
        /// <returns></returns>
        public new ApiResultList<T> Error(ApiResultSubCode subCode)
        {
            base.Error(subCode);
            return this;
        }

        /// <summary>
        ///  autor：tiny
        ///  time：2018-06-20
        ///  description：请求失败
        /// </summary>
        /// <param name="subCode">子状态码</param>
        /// <param name="msg">错误提醒消息</param>
        /// <returns></returns>
        public new ApiResultList<T> Error(ApiResultSubCode subCode, string msg)
        {
            base.Error(subCode, msg);
            return this;
        }

        #endregion
    }
}
